import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

// initial state
const state = {
  modal: {
    show: false,
    addressShow: false, // 是否显示地址modal
    callBack: null, // 每次选择地址的回调事件
    showType: '', // 显示类型，支付密码、选择地址或者设置支付密码
    modalClass: '', // 自定义类名
    title: '', // 模态框标题
    content: '', // 模态框文字内容
    btnClass: '', // 自定义类名
    btnTitle: [], // 按钮内容，一个按钮是字符串，两个按钮时数组
    btnCallback: '', // 事件，一个按钮的事件或者最后一个按钮的事件
    password: '',
    payCallback: null
  },
  toast: {
    show: false,
    message: [],
    icon: null
  },
  loading: false,
  scroll: {
    withTop: 0, // 距离顶部的距离
    withBottom: 0 // 距离底部距离
  },
  isDateShow: false, // 是否显示
  hasDateTrans: false,
  date: {
    selectType: 2, // 选择的类型，1是选择年份，2是选择年月，3是选择年月日，默认年月
    default: [
      [new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()],
      [new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()]
    ], // 可选的时间或者时间段，两个值，年月中的起始和结束值分别是一个数组，默认当前日期到当前日期+1
    showAllText: true, // 是否显示‘年’、‘月’、‘日’，默认显示
    showDoubleNum: true, // 是否显示个位月份和个位天数前面的‘0’，默认不显示
    selectResult: [],
    canRepeat: false, // 起始值和结束值是否可以一样
    intervalTimeType: '' // 间隔时间的类型，分为start、end
  },
  phone: '',
  trueName: '',
  uId: ''
};

// getters
const getters = {
};

// actions
const actions = {
};

// mutations
const mutations = {
  // 显示modal
  showModal: (state, data = true) => {
    if (data) {
      for (let key in data) {
        state.modal[key] = data[key];
      }
      state.modal.show = true;
    }
  },
  // 隐藏modal
  hideModal: state => {
    state.modal.show = false;
    state.modal.addressShow = false;
    state.modal.showType = '';
    state.modal.password = '';
  },
  // 密码赋值
  setPassword: (state, pass) => {
    state.modal.password = pass;
  },
  showToast: (state, data) => {
    state.toast = data;
    state.toast.show = true;
    let time;
    if (state.toast.message.length > 30) {
      time = 2400;
    } else {
      time = 2000;
    }
    let timeout = setTimeout(() => {
      state.toast.show = false;
      clearTimeout(timeout);
    }, time);
  },
  hideToast: (state) => {
    state.toast.show = false;
  },
  showLoading: (state, data = true) => {
    if (data) {
      let timeOut = setTimeout(() => {
        state.loading = data;
        clearTimeout(timeOut);
      }, 0);
    } else {
      state.loading = data;
    }
  },
  setWithTop: (state, data) => {
    state.scroll.withTop = data;
  },
  setwithBottom: (state, data) => {
    state.scroll.withBottom = data;
  },
  configDateWrap: (state, data) => {
    if (data && data.intervalTimeType === 'end' && !state.date.selectResult[0]) {
      Vue.prototype.Toast('请先选择起始日期');
      return false;
    }
    if (data) {
      for (let key in data) {
        state.date[key] = data[key];
      }
    }
    state.isDateShow = true;
    state.hasDateTrans = true;
  },
  showDateWrap: (state) => {
    state.isDateShow = true;
  },
  hideDateWrap: (state) => {
    state.isDateShow = false;
  },
  sureDate: (state, data) => {
    state.date.selectResult = data;
  },
  beDefault: (state, data) => {
    state.date.default = [
      [new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()],
      [new Date().getFullYear(), new Date().getMonth() + 1, new Date().getDate()]
    ];
    state.date.selectResult = [];
  },
  // 获取手机号
  getPhone: (state, phone) => {
    state.phone = phone;
  },
  // 获取真实姓名
  getName: (state, name, id) => {
    state.trueName = name;
    state.uId = id;
  }
};

export default new Vuex.Store({
  state,
  getters,
  actions,
  mutations
});
